package com.cn.lp.export.plugin.common;

import com.cn.lp.common.utils.BlankAide;
import com.cn.lp.export.formatter.LangFormatter;
import com.cn.lp.export.loader.ClassFilter;
import com.cn.lp.export.log.LocalLogger;
import com.cn.lp.export.log.LogHandler;
import com.cn.lp.export.output.CombinationPather;
import com.cn.lp.export.output.Exportor;
import com.cn.lp.export.source.javaparser.JParserSourceEnumValue;
import com.cn.lp.export.table.TableAttribute;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.event.Level;

/** 联合导出任务 */
public abstract class CommonCombinationExportAllProcess implements Process{

    protected final static LocalLogger log = LogHandler.getLog(JParserSourceEnumValue.class);

    /** 输出路径 */
    protected String outPutDirection;

    /** 源文件路径 */
    protected String sourcePath;

    /** class文件路径 */
    protected String[] classPaths;

    /** 搜索的包路径 */
    protected String[] basePackages;

    /** 依赖路径 */
    protected String[] dependencyPaths;

    /** mvel文件地址（系统路径或者ClassLoader下资源名） */
    protected String mvlPath;

    /** mvel表达式 */
    protected String mvlText;

    /** 是否打印参数 */
    protected boolean logParam;

    /** 输出文件名 */
    protected String outPutFileName;

    /** 日记等级 */
    protected String logLevel;

    public CommonCombinationExportAllProcess(String outPutDirection, String sourcePath, String[] classPaths,
                                             String[] basePackages, String[] dependencyPaths,
                                             String mvlPath, String mvlText, boolean logParam,
                                             String outPutFileName, String logLevel) {
        this.outPutDirection = outPutDirection;
        this.sourcePath = sourcePath;
        this.classPaths = classPaths;
        this.basePackages = basePackages;
        this.dependencyPaths = dependencyPaths;
        this.mvlPath = mvlPath;
        this.mvlText = mvlText;
        this.logParam = logParam;
        this.outPutFileName = outPutFileName;
        this.logLevel = logLevel;
    }

    protected abstract TableAttribute createTableAttribute();

    protected abstract ClassFilter createClassFilter();

    public void execute()  {
        Level level = Level.ERROR;
        if(BlankAide.isNotBlank(logLevel)) {
            level = Level.valueOf(logLevel);
            log.info("logLevel : " + level);
        }
        log.info("outPutDirection : " +  outPutDirection);
        log.info("sourcePath : " +  sourcePath);
        log.info("classPaths: -----------");
        if(BlankAide.isNotBlank(classPaths)) {
            for (String classPath : classPaths) {
                log.info(classPath);
            }
        }
        log.info("classPaths: -----------");
        try {
            Exportor exportor = new Exportor()
                    .setLogLevel(level)
                    .setBasePackage(basePackages)
                    // 输出地址
                    .setBaseDir(outPutDirection)
                    // 依赖
                    .setDependencyPath(dependencyPaths)
                    .setSourceClassPath(classPaths)
                    // 源码地址
                    // mvl地址
                    .setBasePackage(basePackages)
                    // 语言
                    .setFormatter(LangFormatter.JAVA)
                    // 扫描过滤器
                    .addFilter(createClassFilter())
                    .setLogParam(logParam)
                    // 构建对象
                    .setCreator(this::createTableAttribute);
            if(!StringUtils.isBlank(sourcePath)) {
                exportor.setSourceJavaPath(sourcePath);
            }
            if(BlankAide.isNotBlank(mvlText)) {
                exportor.setCombinationMvlText(() -> mvlText);
            } else {
                exportor.setCombinationMvlPath(createMvlPath());
            }
            exportor.setOutputCombinationFileName(createOutputFileName())
                    .combinationExportAll();
        } catch (Exception e) {
            log.error("导出异常", e);
        }
    }

    protected CombinationPather createMvlPath() {
        return () -> {
            if(BlankAide.isNotBlank(mvlPath)) {
                return mvlPath;
            } else {
                return "CombinationEnum.mvl";
            }
        };
    }

    protected CombinationPather createOutputFileName() {
        return () -> {
            if(BlankAide.isNotBlank(outPutFileName)) {
                return outPutFileName;
            } else {
                return "CombinationEnum.java";
            }
        };
    }

}

